﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Tin18.Library
{
    public class MyLibrary
    {
        public static string Base64(string _s)
        {
            string s = _s.Trim();
            var bytes = Encoding.UTF8.GetBytes(s);
            var base64 = System.Convert.ToBase64String(bytes);
            string ss = HttpUtility.UrlEncode(base64.ToString());
            return ss;
        }

        public static string RanDomBase64()
        {
            var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            var stringChars = new char[11];
            var random = new Random();
            for (int i = 0; i < stringChars.Length; i++)
            {
                stringChars[i] = chars[random.Next(chars.Length)];
            }

            var finalString = new String(stringChars);
            return finalString;
        }

        public static string GetMD5(string s)
        {
            byte[] bs = System.Text.Encoding.UTF8.GetBytes(s);
            MD5Managed md5 = new MD5Managed();
            byte[] hash = md5.ComputeHash(bs);
            StringBuilder sb = new StringBuilder();
            foreach (byte b in hash)
            {
                sb.Append(b.ToString("x2").ToLower());
            }
            return sb.ToString();
        }

    }
}
